﻿namespace Microsoft.VisualStudio.TextTemplating.CommandLine
{
    using Microsoft.VisualStudio.Modeling;
    using System;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Text;

    public static class TextTransform
    {
        internal const string AppName = "TextTransform";

        private static log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        [STAThread]
        public static int MainText(string[] arguments,StringBuilder mess)
        {
            Command command = null;
            int returnCode;
            CommandLineHost host=null;
            try
            {              
                CommandFactory factory = new CommandFactory(Assembly.GetExecutingAssembly(), new StandardOptionParser("TextTransform"));
                try
                {
                    command = factory.Create(arguments);
                    host = (CommandLineHost)command;
                    //host.Parameters.Add("!!tblName!tblACF_UpdateList");
                    //host.Parameters.Add("!!Namespace!test.gen");
                    //host.Parameters.Add(@"!!ConnStr!Password=password;Persist Security Info=True;User ID=sa;Initial Catalog=TAMIC4DB;Data Source=192.168.40.128\SQLEXPRESS");
                    //host.IncludeDirectories.Add(AppDomain.CurrentDomain.BaseDirectory);
                    //host.ReferencePaths.Add(AppDomain.CurrentDomain.BaseDirectory);
                    //m_log.Debug(AppDomain.CurrentDomain.BaseDirectory);
                    ////host.ReferencePaths.Add("Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll");
                    //    //host.References.Add("Microsoft.VisualStudio.TextTemplating.Interfaces");
                    //host.References.Add("Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll");
                    //host.References.Add("Microsoft.VisualStudio.TextTemplating.10.0.dll");
                }
                catch (UsageException exception)
                {
                    if (exception.Command != null)
                    {
                        exception.Command.Specification.PrintFullUsage(Console.Error);
                    }
                    mess.AppendLine(exception.ToString());
                    m_log.Error(mess.ToString());
                    Console.WriteLine(mess);
                    return 1;

                }
                catch (FileNotFoundException exception2)
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error: {0}: {1}", new object[] { exception2.Message, exception2.FileName }));
                    mess.AppendLine(exception2.ToString());
                    Console.WriteLine(mess);
                    m_log.Error(mess.ToString());
                    return 1;
                }
                catch (Exception exception33)
                {
                    mess.AppendLine(exception33.ToString());
                    Console.WriteLine(mess);
                    m_log.Error(mess.ToString());
                    return 1;
                }
                command.Run();                
                command.Dispose();
                returnCode = command.ReturnCode;
                mess.AppendLine("return code:["+returnCode+"]");
                m_log.Error(mess.ToString());
                
            }
            catch (Exception exception3)
            {
                m_log.Error(exception3.ToString());
                if (CriticalException.IsCriticalException(exception3))
                {
                    throw;
                }
                StringBuilder builder = new StringBuilder("Error: ");
                builder.AppendLine(exception3.Message);
                foreach (object obj2 in exception3.Data.Keys)
                {
                    builder.AppendFormat("{0}: {1}", obj2, exception3.Data[obj2]);
                    builder.AppendLine();
                }
                m_log.Error(builder.ToString());
                Console.Error.WriteLine(builder.ToString());
                returnCode = 1;
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
            }
            return returnCode;
        }

        [STAThread]
        public static int Main(string[] arguments)
        {
            Command command = null;
            int returnCode;
            CommandLineHost host = null;
            try
            {
                string mess = "";
                CommandFactory factory = new CommandFactory(Assembly.GetExecutingAssembly(), new StandardOptionParser("TextTransform"));
                try
                {
                    command = factory.Create(arguments);
                    host = (CommandLineHost)command;
                    host.Parameters.Add("!!tblName!tblACF_UpdateList");
                    host.Parameters.Add("!!Namespace!test.gen");
                    host.Parameters.Add(@"!!ConnStr!Password=password;Persist Security Info=True;User ID=sa;Initial Catalog=TAMIC4DB;Data Source=192.168.40.128\SQLEXPRESS");
                    host.IncludeDirectories.Add(AppDomain.CurrentDomain.BaseDirectory+"/dll");
                    host.ReferencePaths.Add(AppDomain.CurrentDomain.BaseDirectory+"/dll");
                    m_log.Debug(AppDomain.CurrentDomain.BaseDirectory);
                    //host.ReferencePaths.Add("Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll");
                    host.References.Add("Microsoft.VisualStudio.TextTemplating.Interfaces");
                    //host.References.Add("Microsoft.VisualStudio.TextTemplating.Interfaces.10.0.dll");
                }
                catch (UsageException exception)
                {
                    if (exception.Command != null)
                    {
                        exception.Command.Specification.PrintFullUsage(Console.Error);
                    }
                    mess=exception.ToString();
                    Console.WriteLine(mess);
                    m_log.Error(mess.ToString());
                    return 1;

                }
                catch (FileNotFoundException exception2)
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error: {0}: {1}", new object[] { exception2.Message, exception2.FileName }));
                    mess=exception2.ToString();
                    Console.WriteLine(mess);
                    m_log.Error(mess.ToString());
                    return 1;
                }
                catch (Exception exception33)
                {
                    mess=exception33.ToString();
                    Console.WriteLine(mess);
                    return 1;
                }
                command.Run();
                command.Dispose();
                returnCode = command.ReturnCode;
                //mess.AppendLine("return code:[" + returnCode + "]");

            }
            catch (Exception exception3)
            {
                m_log.Error(exception3.ToString());
                if (CriticalException.IsCriticalException(exception3))
                {
                    throw;
                }
                StringBuilder builder = new StringBuilder("Error: ");
                builder.AppendLine(exception3.Message);
                foreach (object obj2 in exception3.Data.Keys)
                {
                    builder.AppendFormat("{0}: {1}", obj2, exception3.Data[obj2]);
                    builder.AppendLine();
                }
                Console.Error.WriteLine(builder.ToString());
                returnCode = 1;
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
            }
            return returnCode;
        }

        [STAThread]
        public static int CallT4(string[] arguments,string[] paremeterList,StringBuilder mess)
        {
            Command command = null;
            int returnCode;
            CommandLineHost host = null;
            try
            {
                
                CommandFactory factory = new CommandFactory(Assembly.GetExecutingAssembly(), new StandardOptionParser("TextTransform"));
                try
                {
                    command = factory.Create(arguments);
                    host = (CommandLineHost)command;
                    foreach (string tmp in paremeterList)
                    {
                        host.Parameters.Add(tmp);
                    }
                    host.IncludeDirectories.Add(AppDomain.CurrentDomain.BaseDirectory);
                }
                catch (UsageException exception)
                {
                    if (exception.Command != null)
                    {
                        exception.Command.Specification.PrintFullUsage(Console.Error);
                    }
                    mess.AppendLine(exception.ToString());
                    Console.WriteLine(mess);
                    m_log.Error(mess.ToString());
                    return 1;

                }
                catch (FileNotFoundException exception2)
                {
                    Console.Error.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error: {0}: {1}", new object[] { exception2.Message, exception2.FileName }));
                    mess.AppendLine(exception2.ToString());
                    Console.WriteLine(mess);
                    m_log.Error(mess.ToString());
                    return 1;
                }
                catch (Exception exception33)
                {
                    mess.AppendLine(exception33.ToString());
                    Console.WriteLine(mess);
                    m_log.Error(mess.ToString());
                    return 1;
                }
                command.Run();
                command.Dispose();
                returnCode = command.ReturnCode;
                mess.AppendLine("return code:["+returnCode +"]");
            }
            catch (Exception exception3)
            {
                m_log.Error(exception3.ToString());
                if (CriticalException.IsCriticalException(exception3))
                {
                    throw;
                }
                StringBuilder builder = new StringBuilder("Error: ");
                builder.AppendLine(exception3.Message);
                foreach (object obj2 in exception3.Data.Keys)
                {
                    builder.AppendFormat("{0}: {1}", obj2, exception3.Data[obj2]);
                    builder.AppendLine();
                }
                Console.Error.WriteLine(builder.ToString());
                returnCode = 1;
            }
            finally
            {
                if (command != null)
                {
                    command.Dispose();
                }
            }
            return returnCode;
        }
    }
}

